/**
 * 
 */
package org.vectrics.recruiter.struts.admin.file;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Category;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.vectrics.SystemException;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.document.CandidateDocument;
import org.vectrics.recruiter.document.CandidateDocumentCategory;
import org.vectrics.recruiter.job.JobCandidate;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.recruiter.user.Resume;
import org.vectrics.user.Person;

/**
 * @author pgoette
 * Clase que borra el archivo pasado como parametro.
 */
public class FileRemoveAction extends Action{
	
	
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    		HttpServletRequest request, HttpServletResponse response)
    		throws Exception {
          
    	  // Tomo la id del documento.
    	  String idDocument = request.getParameter("documentId");
    	  // Si el id de document es nulo devuelvo el mensaje de error y cancelo la operacion
    	  if ((idDocument==null)||(idDocument.trim().equals(""))) {
    		  ActionErrors errors = new ActionErrors();
			  errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.remove.file.id.is.required"));
			  this.saveErrors(request, errors);
    		  return mapping.findForward("success");
    	  }
    	  // Recupero el documento
    	  CandidateDocument document = RecruitServiceLocator
    	              .getRecruitUserService().findCandidateDocument(new Long(idDocument));
    	  
    	  // Si es un resume me fijo si lo estan usando 
    	  if (document.getCategory().getCode().equals(CandidateDocumentCategory.CATEGORY_RESUME)) {
    		  Resume resume = RecruitServiceLocator
                   .getRecruitUserService().findResume(document);

    		  if (resume!= null) {
    			  List<JobCandidate> jobCandidates = RecruitServiceLocator
    			  .getJobProvider().findJobCandidateForResume(resume.getId());
    			  
    			  if ((jobCandidates!=null)&&(!jobCandidates.isEmpty())) {
    				  ActionErrors errors = new ActionErrors();
    				  errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("error.remove.file.in.use"));
    				  this.saveErrors(request, errors);
    				  return mapping.findForward("success");
    			  }    		
    			  RecruitServiceLocator.getRecruitUserService().removeResume(resume);
    		  }
    	  }
    	  
    	  // Elimino el documento.
    	  try {    		  
    		  RecruitServiceLocator.getRecruitUserService().removeDocument(document);
    	  } catch (Exception e) {
    		  ActionErrors errors = new ActionErrors();
			  errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("error.remove.file"));
			  this.saveErrors(request, errors);
		  }
    	  
    	  
          return mapping.findForward("success");
    }

	
	
}
